package com.haiyangwang1.calculator;

import com.haiyangwang1.flychess.Map;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame{
    private JTextField txtInput;
    private JButton btn;
    private JPanel p1;
    private String op;
    private Boolean flag=false;
    private double num1,num2;
    private String result;
    public Calculator(){
        //写一个窗口
        JFrame myframe = new JFrame();
        myframe.setBounds(600,200,400,600);
        txtInput = new JTextField(40);
        myframe.add(txtInput,BorderLayout.NORTH);
        //输入框不能点击，初始光标在左边，让光标到右边去
        txtInput.setEnabled(false);
        txtInput.setHorizontalAlignment(JTextField.RIGHT);
        p1 = new JPanel();

        p1.setLayout(new GridLayout(4,4,5,5));
        p1.add(getButton("7"));
        p1.add(getButton("8"));
        p1.add(getButton("9"));
        p1.add(getButton("*"));
        p1.add(getButton("4"));
        p1.add(getButton("5"));
        p1.add(getButton("6"));
        p1.add(getButton("/"));
        p1.add(getButton("1"));
        p1.add(getButton("2"));
        p1.add(getButton("3"));
        p1.add(getButton("+"));
        p1.add(getButton("0"));
        p1.add(getButton("."));
        p1.add(getButton("="));
        p1.add(getButton("-"));


//        myframe.add(btn);


//      写一个拿到按钮然后做什么事情的方法
        myframe.add(p1);
        myframe.setVisible(true);
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private JButton getButton(String value1){
        btn = new JButton(value1);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //点击按钮然后把值传给上面的框框
                //得到按钮上面的值

                if (flag){//当计算过一次之后做以下的事情
                    txtInput.setText("");
                    num1 = 0;
                    num2 = 0;
                    result = "0";
                    op = "";
                    flag = false;
                }
                String str = e.getActionCommand();
                switch (str) {
                    case "0":
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        //当输入0的时候
                        if (txtInput.getText().equals("") && str.equals("0")) {
                            txtInput.setText("0");
                        } else if (txtInput.getText().equals("0")) {
                            return;//当是0就不输出后面的
                        } else {
                            txtInput.setText(txtInput.getText() + str);
                        }

                        break;
                        //加减乘除的情况
                    case ".":
                        if (txtInput.getText().contains(".")){
                            return;
                        }else if(txtInput.getText().equals("")){
                            txtInput.setText("0.");
                            return;

                        }
                        txtInput.setText(txtInput.getText()+str);
                        break;
                    case "+":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "+";
                        txtInput.setText("");
                        break;
                    case "-":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "-";
                        txtInput.setText("");
                        break;
                    case "*":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "*";
                        txtInput.setText("");
                        break;
                    case "/":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "/";
                        txtInput.setText("");
                        break;

                    case "=":
                        num2 = Double.parseDouble(txtInput.getText());
                        switch (op){

                            case "+":

                                result = num1 + num2 + "";
                                break;
                            case "-":

                                result = num1 - num2 + "";
                                break;
                            case "/":
                                if (num2 == 0) {
                                result = "错误";
                                }else {
                                    result = num1 / num2 + "";
                                }
                                break;
                            case "*":

                                result = num1 * num2 + "";
                                break;
                        }
                        txtInput.setText(result);
                        flag = true;
                        break;

                }


            }
        });
        return btn;
    }

    public static void main(String[] args) {
        Calculator calculator = new Calculator();

    }
}
